package com.xteam.ecsystem.dao.Impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xteam.ecsystem.dao.MemberDao;
import com.xteam.ecsystem.entities.BaseMemberBase;
import com.xteam.ecsystem.utils.PageCenterStation;
import com.xteam.ecsystem.utils.StringUtil;

@Repository
@SuppressWarnings("unchecked")
public class MemberDaoImpl extends DDLPrefix implements MemberDao {
	
	@Autowired
	private BaseDaoImpl baseDao;

	public String getMemberList(int pageIndex, int pageSize, String bmbNick, String bgttel,
			String bgtState) {
		JSONObject jo = new JSONObject();
		List list = new ArrayList();
		String sql = "";
		String countSql = "";
		
		try {
			sql = "select bmb.bmb_id as bmbId, bmb.bmb_nick as bmbNick, bmb.bmb_email as bmbEmail, "
				+ "bmb.bmb_mobile as bmbMobile, bmb.bmb_password as bmbPassword, bmb.bmb_question as "
				+ "bmbQuestion, bmb.bmb_answer as bmbAnswer, bmb.bmb_last as bmbLast, bmb.bmb_state as "
				+ "bmbState, bmb.bmb_activator as bmbActivator, bmb.bmb_integral as bmbIntegral, "
				+ "bmb.bmb_smnotify as bmbSmnotify, bmb.bmb_emnotify as bmbEmnotify, bmb.bmb_grade as "
				+ "bmbGrade, bmb.bmb_buysnew as bmbBuysnew, bmb.bmb_buystotal as bmbBuystotal, "
				+ "bmb.bmb_upgradetime as bmbUpgradetime, bgr.bgr_icon as bgrIcon "
				+ "from base_member_base as bmb left join base_grade_rule as bgr "
				+ "on bmb.bmb_grade = bgr.bgr_id where 1 = 1 ";
			
			countSql = "select count(*) from base_member_base as bmb left join base_grade_rule as bgr "
				+ "on bmb.bmb_grade = bgr.bgr_id where 1 = 1 ";
			
			if (StringUtil.isNotNullOrEmpty(bmbNick)) {
				sql += "and bmb.bmb_nick like ? ";
				countSql += "and bmb.bmb_nick like ? ";
				list.add("%" + bmbNick + "%");
			}

			if (StringUtil.isNotNullOrEmpty(bgttel)) {
				sql += "and bmb.bmb_mobile like ? ";
				countSql += "and bmb.bmb_mobile like ? ";
				list.add("%" + bgttel + "%");
			}
			
			if (StringUtil.isNotNullOrEmpty(bgtState) && !bgtState.equals("-1")) {
				sql += "and bmb.bmb_state = ? ";
				countSql += "and bmb.bmb_state = ? ";
				list.add(Integer.parseInt(bgtState));
			}
			
			if (true) {
				sql += "order by bmb.bmb_grade desc ";
			}
			
			PageCenterStation<?> p = (PageCenterStation<?>) this.baseDao
				.queryWithSQL(BaseMemberBase.class, countSql, sql, 
						null, list.toArray(), pageIndex * pageSize, pageSize);
			
			jo = JSONObject.fromObject(p);

//			String hql = SQL_FROM + "BaseMemberBase" + SQL_WHERE + " 1=1 ";
//			String countHQL = SQL_SELECT + " count(*) " + SQL_FROM + "BaseMemberBase" + SQL_WHERE + " 1=1 ";
//
//			if (StringUtil.isNotNullOrEmpty(bmbNick)) {
//				hql += SQL_AND + " bmbNick " + SQL_LIKE + " ? ";
//				countHQL += SQL_AND + " bmbNick " + SQL_LIKE + " ? ";
//				list.add("%" + bmbNick + "%");
//			}
//
//			if (StringUtil.isNotNullOrEmpty(bgttel)) {
//				hql += SQL_AND + " bmbMobile " + SQL_LIKE + " ? ";
//				countHQL += SQL_AND + " bmbMobile " + SQL_LIKE + " ? ";
//				list.add("%" + bgttel + "%");
//			}
//			
//			if (StringUtil.isNotNullOrEmpty(bgtState) && !bgtState.equals("-1")) {
//				hql += SQL_AND + " bmbState =? ";
//				countHQL += SQL_AND + " bmbState =? ";
//				list.add(Integer.parseInt(bgtState));
//			}
//			
//			PageCenterStation<?> p = (PageCenterStation<?>) this.baseDao.queryList(countHQL, hql, 
//					list.toArray(), pageIndex * pageSize, pageSize);
//			
//			jo = JSONObject.fromObject(p);

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return jo.toString();
	}

	public String getQuestionList() {
		JSONArray jo = new JSONArray();
		
		try {
			List list = new ArrayList();
			String hql = SQL_FROM + "BasePasswordQuestion" + SQL_WHERE + " bpqState=1";
			list = baseDao.find(hql);
			jo = JSONArray.fromObject(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return jo.toString();
	}

	public static String getUUID() {
		String s = UUID.randomUUID().toString();
		// 去掉“-”符号
		return s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18) + s.substring(19, 23)
				+ s.substring(24);
	}

	public void saveOrUpdateMember(Object obj, String id) {
		BaseMemberBase member = (BaseMemberBase) obj;

		if (id != null && !"".equals(id) && id.length() > 0) {
			member.setBmbLast(new Date());
			baseDao.update(member);
		} else {
			member.setBmbLast(new Date());
			member.setBmbActivator(getUUID());
			baseDao.save(member);
		}

	}

	public void delMember(String bmbIdlist) {
		String oId[] = bmbIdlist.split(",");
		
		for (String id : oId) {
			String sql = "delete from base_member_base where bmb_id in(" + id + ")";
			baseDao.executeUpdateWithSQL(sql, (Object[]) null);
		}
	}

	public void operateMember(String bmbIdlist, String type) {
		String sql = "update base_member_base set bmb_state=" + Integer.parseInt(type)
				+ " where bmb_id in(" + bmbIdlist.substring(0, bmbIdlist.length() - 1) + ")";
		
		baseDao.executeUpdateWithSQL(sql, (Object[]) null);
	}
}
